//
//  Fraction.m
//  OC03_属性_点语法
//
//  Created by 侯平章 on 16/2/21.
//  Copyright © 2016年 侯平章. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
#pragma mark -初始化
-(instancetype)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator{
    self = [super init];
    if (self) {
        _numerator = numerator;
        _denominator = denominator;
    }
    return self;
}

#pragma mark -构造器
+(instancetype)fractionWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator{
    return [[Fraction alloc] initWithNumerator:numerator denominator:denominator];
}

#pragma mark -功能
-(Fraction *)plusWithFraction:(Fraction *)frac{
    //新的分数分子
    NSInteger newNum = self.numerator*frac.denominator+self.denominator*frac.numerator;
    //新的分数分母
    NSInteger newDeno = self.denominator*frac.denominator;
    Fraction *newFrac = [Fraction fractionWithNumerator:newNum denominator:newDeno];
    return newFrac;
}
- (Fraction *)subWithFraction:(Fraction*)frac{
//分子
    NSInteger newNum= self.numerator*frac.denominator-self.denominator*frac.numerator;
//分母
    NSInteger newDen=self.denominator*frac.denominator;
//新的分数
    Fraction *newFrac = [Fraction fractionWithNumerator:newNum denominator:newDen];
    return newFrac;
}

//乘法
-(Fraction *)multWithFraction:(Fraction *)frac{
//分子
    NSInteger newNum = self.numerator*frac.numerator;
    NSInteger newDen = self.denominator *frac.denominator;
    Fraction *newFrac = [Fraction fractionWithNumerator:newNum denominator:newDen];

    return newFrac;
    
}
//除法
-(Fraction *)divWithFraction:(Fraction *)frac{
    NSInteger newNum = self.numerator*frac.denominator;
    NSInteger newDen = self.denominator*frac.numerator;
    Fraction *newFrac = [Fraction fractionWithNumerator:newNum denominator:newDen];
    return newFrac;

}

//比较大小
-(Fraction *)comWithFraction:(Fraction *)frac{
//    NSInteger newDen = self.denominator*frac.denominator;
    NSInteger newSelfNum = self.numerator*frac.denominator;
    NSInteger newFracNum = frac.numerator*self.denominator;
    if (newSelfNum - newFracNum >0) {
        return self;
    }else if (newSelfNum - newSelfNum <0){
        return frac;
    }else{
        return 0;
    }

}
//约分
-(Fraction *)reductionWithFraction:(Fraction *)frac{
    NSInteger selfMin = self.denominator<self.numerator?self.denominator:self.numerator;
    NSInteger fracMin = self.denominator<self.numerator?self.denominator:self.numerator;
    for (NSInteger i =selfMin; i>0; i--) {
        if (self.numerator%i == 0&&self.denominator%i == 0) {
            Fraction *newFrac = [Fraction fractionWithNumerator:self.numerator/i denominator:self.denominator/i];
            return newFrac;
        }
    }
    for (NSInteger i = fracMin; i>0; i--) {
        if (frac.denominator%i == 0&&frac.numerator%i == 0) {
            Fraction *newFrac1 = [Fraction fractionWithNumerator:frac.numerator/i denominator:frac.denominator/i];
            return newFrac1;
        }
    }
    return 0;
}


@end
